package com.heartech.prediciton_wx.service.message;

import com.heartech.prediciton_wx.dao.MessageDTO;
import com.heartech.prediciton_wx.entity.message.BaseMessage;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
@Data
public class MessageAttributeSetter {
    // 第一个参数 为MessageDao的私有属性给实体基类进行赋值
    // 第二个参数 为继承自BaseMessage基类的子类，用来向下转型
    public static <E extends BaseMessage> E setTextAttribute(MessageDTO messageDTO, Class<E> eClass) throws IllegalAccessException, InstantiationException {
        BaseMessage baseMessage = eClass.newInstance();
        baseMessage.setCreateTime(new Date().getTime());
        baseMessage.setFromUserName(messageDTO.getFromUserName());
        baseMessage.setMsgId(messageDTO.getMsgId());
        baseMessage.setToUserName(messageDTO.getToUserName());
        baseMessage.setMsgType(messageDTO.getMsgType());
        log.warn("MessageAttributeSetter返回值如下：\n" + baseMessage.toString());

        return (E) baseMessage;
    }

    public static <E extends BaseMessage> E setEventAttribute(MessageDTO messageDTO, Class<E> eClass) throws IllegalAccessException, InstantiationException {
        BaseMessage baseMessage = eClass.newInstance();
        baseMessage.setCreateTime(new Date().getTime());
        baseMessage.setFromUserName(messageDTO.getFromUserName());
        baseMessage.setToUserName(messageDTO.getToUserName());
        baseMessage.setMsgType(messageDTO.getMsgType());
        baseMessage.setEvent(messageDTO.getEventType());
        baseMessage.setEventKey(messageDTO.getEventkey());
        baseMessage.setTicket(messageDTO.getTicket());
        log.warn("MessageAttributeSetter返回值如下：\n" + baseMessage.toString());

        return (E) baseMessage;
    }
}
